<script setup>
import { ref, computed, onMounted } from "vue";
import ViewTeacher from "@/components/shared/ViewTeacher.vue";
import { getTeacherListService, teacherDelService, editTeacherPwdService } from "@/api/user";

const loading = ref(false);
const drawer = ref();
const userData = ref([]);
const selectedData = ref([]);
const pageSize = ref(15);
const currentPage = ref(1);
const dialogVisible = ref(false);
const updateData = ref({});

const getTeacherList = async () => {
  loading.value = true;
  const res = await getTeacherListService();
  userData.value = res.data.teacher;
  selectedData.value = userData.value;
  loading.value = false;
};

onMounted(() => {
  getTeacherList();
});
const onEdit = (row) => {
  drawer.value.open(row);
};
const onDel = async (row) => {
  await ElMessageBox.confirm("你确认删除该老师信息吗？", "温馨提示", {
    type: "warning",
    confirmButtonText: "确认",
    cancelButtonText: "取消",
  });
  await teacherDelService(row._id);
  ElMessage({ type: "success", message: "删除成功" });
  getTeacherList();
};
const updatePwd = async () => {
  await ElMessageBox.confirm("你确认修改该老师密码吗？", "温馨提示", {
    type: "warning",
    confirmButtonText: "确认",
    cancelButtonText: "取消",
  });
  await editTeacherPwdService({id: updateData.value._id, teacher: updateData.value});
  ElMessage({ type: "success", message: "修改成功" });
  dialogVisible.value = false;
  updateData.value = {};
  getTeacherList();
};
const updatePwdModal = (row) => {
  dialogVisible.value = true;
  updateData.value = row;
};
const openDrawer = () => {
  drawer.value.open({});
};

const onSuccess = () => {
  getTeacherList();
};
const paginatedData = computed(() => {
  const start = (currentPage.value - 1) * pageSize.value;
  const end = start + pageSize.value;
  return selectedData.value.slice(start, end);
});
const handlePageChange = (pageNum) => {
  currentPage.value = pageNum;
  getTeacherList();
};
const handleSizeChange = (size) => {
  pageSize.value = size;
  getTeacherList();
};
</script>

<template>
  <page-container title="教师管理">
    <template #extra>
      <el-button type="primary" @click="openDrawer">添加教师</el-button>
    </template>
    <el-table :data="paginatedData" style="width: 100%" v-loading="loading">
      <el-table-column label="教工工号" prop="code"></el-table-column>
      <el-table-column label="教工姓名" prop="name"></el-table-column>
      <el-table-column label="手机号" prop="phone"></el-table-column>
      <el-table-column label="操作" width="220">
        <template #default="{ row }">
          <el-button size="small" type="warning" @click="updatePwdModal(row)"
            >修改密码</el-button
          >
          <el-button size="small" @click="onEdit(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="onDel(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination
      background
      layout="total,prev, pager, next"
      :total="selectedData.length"
      @current-change="handlePageChange"
      @size-change="handleSizeChange"
      :current-page="currentPage"
      :page-size="pageSize"
      style="margin-top: 20px; justify-content: flex-end"
    />
    <view-teacher ref="drawer" @success="onSuccess"></view-teacher>
    <el-dialog
      title="提示"
      v-model="dialogVisible"
      width="30%"
      :before-close="handleClose"
    >
    <el-form label-width="100px" :model="updateData">
      <el-form-item label="密码">
        <el-input v-model="updateData.password" type="password" placeholder="请输入新密码"></el-input>
      </el-form-item>
    </el-form>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updatePwd()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </page-container>
</template>

<style scoped></style>